const { contextBridge, ipcRenderer } = require('electron')

// 打开一个新窗口
const createNewWindow = () => {
    ipcRenderer.invoke('create-new-window')
}

// 关闭新的窗口
const closeNewWindow = (sign) => {
    ipcRenderer.invoke('close-new-window', sign)
}

// 关闭主窗口
const closeMainWindow = () => {
    ipcRenderer.invoke('close-main-window')
}

// 最小化主窗口
const minMainWindow = () => {
    ipcRenderer.invoke('min-main-window')
}

// 保存窗口大小
const saveWindowSize = (size) => {
    ipcRenderer.send('save-window-size', size)
}

// 打开mp3
const uploadMp3 = async () => {
    const res = await ipcRenderer.invoke('upload-mp3')
    return res
}

// 获取保存路径下的所有mp3文件名
const getAllMp3FileName = async () => {
    let filesName = await ipcRenderer.invoke('get-all-mp3-file-name')
    return filesName
}

// 获取保存路径下的所有mp3文件的绝对路径 
const getAllMp3FileResolve = async () => {
    let filesResolve = await ipcRenderer.invoke('get-all-mp3-file-resolve')
    return filesResolve
}

// 读取本地路径中的mp3文件并转换为临时URL
const readMp3FileToDataUrlByResolve = async (resolve) => {
    let dataUrl = await ipcRenderer.invoke('read-mp3-file-to-url', resolve)
    return dataUrl
}

// 删除某个路径下的mp3文件
const deleteMp3FileInDirectory = async (fileResolve) => {
    const res = await ipcRenderer.invoke('delete-mp3-in-directory', fileResolve)
    return res
}

// 将特定的Electron API暴露给渲染进程
contextBridge.exposeInMainWorld('myApi', {
    createNewWindow,
    closeNewWindow,
    closeMainWindow,
    minMainWindow,
    saveWindowSize,
    uploadMp3,
    getAllMp3FileName,
    getAllMp3FileResolve,
    readMp3FileToDataUrlByResolve,
    deleteMp3FileInDirectory
})